package com.bysj.lms.service.impl;

import com.bysj.lms.pojo.entity.DictionaryItem;
import com.bysj.lms.pojo.entity.DictionaryType;
import com.bysj.lms.mapper.DictionaryItemMapper;
import com.bysj.lms.service.IDictionaryItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bysj.lms.service.IDictionaryTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */
@Service
public class DictionaryItemServiceImpl extends ServiceImpl<DictionaryItemMapper, DictionaryItem> implements IDictionaryItemService {

    @Autowired
    DictionaryItemMapper dictionaryItemMapper;
    @Autowired
    IDictionaryTypeService dictionaryTypeService;

    @Override
    public PageInfo findByPage(int typeId, int page, int count) {
        PageHelper.startPage(page, count);
        List<DictionaryItem> list = lambdaQuery().eq(DictionaryItem::getTypeId, typeId)
                .eq(DictionaryItem::getStatus, 0).list();
        return new PageInfo(list);
    }

    @Override
    public DictionaryItem find(int typeId, String name) {

        DictionaryItem one = lambdaQuery().eq(DictionaryItem::getTypeId, typeId)
                .eq(DictionaryItem::getName, name).one();
        return one;
    }

    @Override
    public boolean updateStatus(int id,int status) {
        boolean update = lambdaUpdate().eq(DictionaryItem::getId,id)
                .set(DictionaryItem::getStatus, status).update();
        return update;
    }

    @Override
    public List<DictionaryItem> findByType(String type) {

        DictionaryType dictionaryType = dictionaryTypeService.findByType(type);
        List<DictionaryItem> list = lambdaQuery().eq(DictionaryItem::getTypeId, dictionaryType.getId())
                .eq(DictionaryItem::getStatus, 0).list();
        return list;
    }
    
    @Override
    public DictionaryItem find(String type, String value) {
        DictionaryType dictionaryType = dictionaryTypeService.findByType(type);
        DictionaryItem one = lambdaQuery().eq(DictionaryItem::getTypeId, dictionaryType.getId())
                .eq(DictionaryItem::getValue, value).one();
        return one;
    }
}
